<template>
  <el-card class="mt-20">
    <el-form>
      <el-form-item label="属性名">
        <el-input
          placeholder=""
          style="width: 200px"
          class="ml-10"
          v-model="attrFormData.attrName"
        ></el-input>
      </el-form-item>
      <el-button
        type="primary"
        :icon="Plus"
        :disabled="!attrFormData.attrName"
        @click="addAttrValue"
        >添加属性</el-button
      >
      <el-button> 取消</el-button>

      <el-table :data="attrFormData.attrValueList" style="width: 100%">
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="name" label="属性值名称" width="500">
          <template v-slot="{ row, $index }">
            <el-input
              v-if="row.isShowEdit"
              placeholder="请输入内容"
              size="small"
              ref="inputRef"
              @blur="switchShow(row, $index)"
              @keyup.enter="switchShow(row, $index)"
              v-model="row.valueName"
            ></el-input>
            <div v-else="!row.isShowEdit" @click="switchEdit(row)">
              {{ row.valueName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template v-slot="{ row, $index }">
            <el-button
              type="danger"
              :icon="DeleteFilled"
              size="small"
              title="删除"
              @click="delAttrValue($index)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="primary" @click="submit" :disabled="disabled"
        >保存</el-button
      >
      <el-button @click="showAttr"> 取消</el-button>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "addOrUpdateAttr",
};
</script>

<script lang="ts" setup>
import { Plus, DeleteFilled } from "@element-plus/icons-vue";
import { reactive, ref, nextTick, computed } from "vue";
import { ElMessage } from "element-plus";
import type { AttrsItem, AttrValueItem } from "@/api/product/model/attrModel";
import { addAttrListApi } from "@/api/product/attr";
import { useCategoryStore } from "@/stores/category";
import { useAttrStore } from "@/stores/attr";

const categoryStore = useCategoryStore();
const attrStore = useAttrStore();
const inputRef = ref();
//提交保存
const submit = async () => {
  const data = {
    ...attrFormData,
    categoryId: categoryStore.category3Id as number,
    categoryLevel: 3,
  };
  const id = attrStore.attr.id;
  if (id) {
    data.id = id;
  }
  await addAttrListApi(data);
  ElMessage.success(`${id ? "修改" : "添加"}成功`);
  showAttr();
};

//禁用按钮
const disabled = computed(() => {
  return (
    !attrFormData.attrName ||
    !attrFormData.attrValueList.some((attr) => attr.valueName)
  );
});

//表单数据
const attrFormData = reactive<AttrsItem>({
  attrName: attrStore.attr.attrName,
  attrValueList: attrStore.attr.attrValueList,
});
//删除属性
const delAttrValue = (index: number) => {
  attrFormData.attrValueList.splice(index, 1);
};
//添加属性值
const addAttrValue = async () => {
  attrFormData.attrValueList.push({
    isShowEdit: true,
    valueName: "",
  });
  await nextTick();
  inputRef.value.focus();
};

//显示input
const switchShow = (row: AttrValueItem, index: number) => {
  if (!row.valueName) {
    attrFormData.attrValueList.splice(index, 1);
    return;
  }

  const isRepeat = attrFormData.attrValueList.some((attrValue) => {
    if (attrValue === row) return false;
    if (attrValue.valueName === row.valueName) return true;
    return false;
  });
  if (isRepeat) {
    ElMessage.warning("不能输入重复的属性值");
    return;
  }
  row.isShowEdit = false;
};
//显示文本框
const switchEdit = async (row: AttrValueItem) => {
  row.isShowEdit = true;
  await nextTick;
  inputRef.value.focus();
};

const emit = defineEmits(["setIsShowAttrForm"]);

const showAttr = () => {
  emit("setIsShowAttrForm", true);
};
</script>

<style scoped></style>
